<template>
  <div style="width: 50%; margin: 50px auto">
    <div style="display: flex; grid-gap: 100px">
      <img src="@/assets/imgs/music.gif" alt="" style="width: 300px">
      <div style="flex: 1">
        <div style="font-size: 30px; font-weight: 300">{{ data.songData.name }}</div>
        <div style="display: flex; align-items: center; grid-gap: 5px; margin-top: 10px">
          <el-icon size="16"><Avatar /></el-icon>
          <div style="cursor: pointer; font-size: 16px; margin-right: 20px" @click="router.push('/front/singerDetail?id=' + data.songData.singerId)">{{ data.songData.singerName }}</div>
        </div>
        <div style="margin-top: 10px; color: #666666">歌曲分类：{{ data.songData.categoryName }}</div>
        <div style="margin-top: 5px; color: #666666">作词人：{{ data.songData.lyricist }}</div>
        <div style="margin-top: 5px; color: #666666">作曲人：{{ data.songData.composer }}</div>
        <div style="margin-top: 5px; color: #666666">发行时间：{{ data.songData.time }}</div>
        <div style="margin-top: 5px; color: #666666">热度：🔥{{ data.songData.hot }}</div>
        <div style="margin-top: 20px">
          <el-button type="primary" @click="addPlaylistInit(data.songData.id)"><el-icon><FolderAdd /></el-icon><span style="margin-left: 5px">加入歌单</span></el-button>
          <el-button type="warning" v-if="!data.collectData.length" @click="collect"><el-icon><Star /></el-icon><span style="margin-left: 5px">收藏歌曲</span></el-button>
          <el-button type="danger" v-else @click="disCollect"><el-icon><StarFilled /></el-icon><span style="margin-left: 5px">取消收藏</span></el-button>
        </div>
      </div>
    </div>
    <div style="margin-top: 50px">
      <div style="margin-top: 20px">
        <el-input type="textarea" :rows="4" v-model="data.content" placeholder="歌都听了，发表一下你此刻的感受吧！"></el-input>
      </div>
      <div style="margin-top: 10px; text-align: right">
        <el-button type="info" @click="comment">发 布</el-button>
      </div>
      <div style="font-size: 18px">看看大家都说了什么（{{ data.commentData.length }}）</div>
      <div style="margin-top: 30px">
        <div style="display: flex; grid-gap: 20px; margin-bottom: 20px" v-for="item in data.commentData">
          <img :src="item.userAvatar" alt="" style="width: 50px; height: 50px; border-radius: 50%">
          <div style="flex: 1">
            <div style="font-weight: bold">{{ item.userName }}</div>
            <div style="margin-top: 5px; color: #666666">{{ item.content }}</div>
            <div style="margin-top: 5px; color: #999999">{{ item.time }}</div>
          </div>
        </div>
      </div>
    </div>
    <el-dialog title="我的歌单" v-model="data.formVisible" width="40%" destroy-on-close>
      <el-form ref="formRef" :rules="data.rules" :model="data.form" label-width="80px" style="padding: 20px">
        <el-form-item prop="playlistId" label="选择歌单">
          <el-select v-model="data.form.playlistId" placeholder="请选择歌单" style="width: 90%">
            <el-option v-for="item in data.playlistData" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="data.formVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </span>
      </template>
    </el-dialog>

    <MusicPlayer :musicList="data.songList" v-if="data.songList?.length" />
  </div>
</template>

<script setup>
import {reactive, ref} from "vue";
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";
import router from "@/router/index.js";
import MusicPlayer from "@/views/components/MusicPlayer.vue";
const formRef = ref()
const data = reactive({
  user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
  songId: router.currentRoute.value.query.id,
  songData: {},
  songList: [],
  commentData: [],
  content: null,
  collectData: [],
  playlistData: [],
  form: {},
  formVisible: false,
  rules: {
    playlistId: [
      { required: true, message: '请选择歌单', trigger: 'blur' }
    ],
  }
})

const addPlaylistInit = (songId) => {
  request.get('/playlist/selectAll', {
    params: {
      userId: data.user.id
    }
  }).then(res => {
    if (res.code === '200') {
      data.playlistData = res.data
      data.form = {}
      data.form.songId = songId
      data.formVisible = true
    } else {
      ElMessage.error(res.msg)
    }
  })
}

const save = () => {
  formRef.value.validate(valid => {
    if (valid) {
      request.post('/playlistItem/add', data.form).then(res => {
        if (res.code === '200') {
          ElMessage.success('加入成功')
          data.formVisible = false
        } else {
          ElMessage.error(res.msg)
        }
      })
    }
  })
}

const loadSong = () => {
  request.get('/sing/selectById/' + data.songId).then(res => {
    if (res.code === '200') {
      data.songData = res.data
      data.songList.push(data.songData)
      data.songData.hot = data.songData.hot + 1
      request.put('/sing/update', data.songData)
    } else {
      ElMessage.error(res.msg)
    }
  })
}
loadSong()

const comment = () => {
  if (!data.content) {
    ElMessage.warning('请输入评论内容')
    return
  }
  request.post('/comment/add', {
    content: data.content,
    singId: data.songId
  }).then(res => {
    if (res.code === '200') {
      ElMessage.success('评论成功')
      data.content = null
      loadComment()
    } else {
      ElMessage.error(res.msg)
    }
  })
}

const loadComment = () => {
  request.get('/comment/selectAll', {
    params: {
      singId: data.songId
    }
  }).then(res => {
    if (res.code === '200') {
      data.commentData = res.data
    } else {
      ElMessage.error(res.msg)
    }
  })
}
loadComment()

const collect = () => {
  request.post('/collect/add', {
    relId: data.songId,
    type: 'SONG'
  }).then(res => {
    if (res.code === '200') {
      ElMessage.success('收藏成功')
      loadCollect()
    } else {
      ElMessage.error(res.msg)
    }
  })
}

const disCollect = () => {
  request.delete('/collect/delete/' + data.collectData[0].id).then(res => {
    if (res.code === '200') {
      ElMessage.success('取消成功')
      loadCollect()
    } else {
      ElMessage.error(res.msg)
    }
  })
}

const loadCollect = () => {
  request.get('/collect/selectAll', {
    params: {
      userId: data.user.id,
      relId: data.songId,
      type: 'SONG'
    }
  }).then(res => {
    if (res.code === '200') {
      data.collectData = res.data
    } else {
      ElMessage.error(res.msg)
    }
  })
}
loadCollect()
</script>